package com.jjw.route.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.jjw.route.dao.ArticalDao;
import com.jjw.route.oos.OOSService;
import com.jjw.route.to.ArticalTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.jjw.route.entity.ArticalEntity;
import com.jjw.route.service.ArticalService;
import com.jjw.route.utils.PageUtils;
import com.jjw.route.utils.R;
import org.springframework.web.multipart.MultipartFile;


/**
 * 
 *
 * @author jjw
 * @email 2371671097@qq.com
 * @date 2023-05-12 07:45:21
 */
@RestController
@RequestMapping("route/artical")
@CrossOrigin
public class ArticalController {
    @Autowired
    private ArticalService articalService;
    @Autowired
    private ArticalDao articalDao;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("route:artical:list")
    public R list(@RequestParam Map<String, Object> params){
        System.out.println(params.get("page"));
        PageUtils page = articalService.queryPage(params);
        return R.ok().put("page", page);
    }
    @RequestMapping("/list1")
    //@RequiresPermissions("route:artical:list")
    public R list1(@RequestParam Map<String, Object> params){
        PageUtils page = articalService.queryPage1(params);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("route:artical:info")
    public R info(@PathVariable("id") Integer id){
		ArticalEntity artical = articalService.getById(id);

        return R.ok().put("artical", artical);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("route:artical:save")
    public R save(@RequestBody ArticalEntity artical){
		articalService.save(artical);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("route:artical:update")
    public R update(@RequestBody ArticalEntity artical){
		articalService.updateById(artical);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("route:artical:delete")
    public R delete(@RequestBody Integer[] ids){
		articalService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }


    @RequestMapping("/saveData")
    //@RequiresPermissions("route:artical:save")
    public R save(@RequestBody ArticalTo articalTo){
        return articalService.insert(articalTo);
    }

    @PostMapping("/imgUp")
    public R imgUp(@RequestParam("multipartFiles") MultipartFile[] multipartFiles,@RequestParam("content") String content,
                   @RequestParam("userName") String userName) throws Exception {
        List<byte[]> list = new ArrayList<>();
        for (int i = 0; i < multipartFiles.length; i++) {
            list.add(multipartFiles[i].getBytes());
        }
        List<String> urls = OOSService.imgUpLoadByte(list);
        String[] arr = {urls.get(0)};
        ArticalTo articalTo = new ArticalTo();
        articalTo.setImgs(arr);
        articalTo.setUserName(userName);
        articalTo.setContent(content);
        articalTo.setLike(0L);
        articalTo.setTimeInterval(30);
        articalService.insert(articalTo);
        return R.ok();
    }
}
